Passed
Pull Request — master (#56)
by
unknown
03:10 queued 01:29
created

CreateIngestionCommandHandler.execute   A

Complexity

Conditions 2

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
dl 0
loc 15
rs 9.8
c 0
b 0
f 0
1
import { Inject } from '@nestjs/common';
2
import { CommandHandler } from '@nestjs/cqrs';
3
import { Ingestion } from 'src/Domain/Ingestion/Ingestion.entity';
4
import { SchoolNotFoundException } from 'src/Domain/School/Exception/SchoolNotFoundException';
5
import { IIngestionRepository } from 'src/Domain/Ingestion/Repository/IIngestionRepository';
6
import { ISchoolRepository } from 'src/Domain/School/Repository/ISchoolRepository';
7
import { CreateIngestionCommand } from './CreateIngestionCommand';
8
9
@CommandHandler(CreateIngestionCommand)
10
export class CreateIngestionCommandHandler {
11
  constructor(
12
    @Inject('ISchoolRepository')
13
    private readonly schoolRepository: ISchoolRepository,
14
    @Inject('IIngestionRepository')
15
    private readonly ingestionRepository: IIngestionRepository,
16
  ) {}
17
18
  public async execute(command: CreateIngestionCommand): Promise<string> {
19
    const { schoolId } = command;
20
    const school = await this.schoolRepository.findOneById(schoolId);
21
    if (!school) {
22
      throw new SchoolNotFoundException();
23
    }
24
25
    const ingestion = await this.ingestionRepository.save(
26
      new Ingestion(
27
        school
28
      )
29
    );
30
31
    return ingestion.getId();
32
  }
33
}
34